package cn.easybuy.web.pre;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.easybuy.entity.Product;
import cn.easybuy.service.ProductService;
import cn.easybuy.service.impl.ProductServiceImpl;
import cn.easybuy.util.Page;

public class ProductInfoServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id=request.getParameter("id");
		ProductService ps=new ProductServiceImpl();
		if (id==null||id=="") {
			Page page=new Page();
			int totalCount=ps.getTotalCount();
			String currNo=request.getParameter("currNo");
			if (currNo==null||currNo=="") {
				currNo="1";
			}
			page.setCurrentPageNumber(Integer.parseInt(currNo));
			page.setPageSize(10);
			page.setTotalCount(totalCount);
			List<Product> list=ps.getAllProducts(Integer.parseInt(currNo), page.getPageSize());
			page.setProductsList(list);
			String proJSON=JSON.toJSONString(page);
			PrintWriter out=response.getWriter();
			out.print(proJSON);
			out.flush();
			out.close();
			
		}else {
			
			List<Product> list=ps.getProductInfo(Integer.parseInt(id));
			for (Product product : list) {
				request.setAttribute("id", product.getId());
				request.setAttribute("name", product.getName());
				request.setAttribute("price", product.getPrice());
			}
			request.getRequestDispatcher("Product.jsp").forward(request, response);
		}
		
		
		
		
	}

}
